﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using HYFamilyLibDataManager;

namespace Model
{
	public partial class CableTrayFittingSet : System.Windows.Forms.Form
	{
		public CableTrayFittingSet(UIApplication uiApp, Transaction ts)
		{
			this.InitializeComponent();
			this.m_application = uiApp;
			this.doc = this.m_application.ActiveUIDocument.Document;
			this.ts = ts;
		}

		private void CableTrayFittingSet_Load(object sender, EventArgs e)
		{
			this.FillCableTrayFittingEntity();
			this.FillCableTray();
			this.FillCableTrayType();
			this.RegisterSelEvent();
			try
			{
				this.sysDB = FamilyLibDatabase.GetSqliteDB(SubjectType.Electric, this.m_application.Application.VersionNumber);
			}
			catch (Exception ex)
			{
				TaskDialog.Show("盈建科提示", ex.Message);
			}
			this.dbCategory = this.sysDB.CategoryTable["桥架配件"];
		}

		private void button_Click(object sender, EventArgs e)
		{
			this.cbxCableTray.SelectedItem.ToString();
			CableTrayType cableTrayType = (from type in this.cableTrayTypes
			where type.Name == this.cbxCableTrayType.SelectedItem.ToString()
			select type).ToList<Element>()[0] as CableTrayType;
			Button button = sender as Button;
			YJKComboBox hycomboBox = null;
			string fittingTypeName = this.GetFittingTypeName(button.Name, ref hycomboBox);
			List<string> revitDocFitting = this.GetRevitDocFitting(hycomboBox);
			CableTrayFitting cableTrayFitting = new CableTrayFitting(this.m_application, this.dbCategory, fittingTypeName, cableTrayType.ShapeType, revitDocFitting, this.ts);
			cableTrayFitting.ShowDialog();
			if (cableTrayFitting.SelectFamily != null)
			{
				this.FillFitting();
				FamilySymbol familySymbol = null;
				foreach (FamilySymbol familySymbol2 in cableTrayFitting.SelectFamily.GetSymbols())
				{
					if (familySymbol2 != null)
					{
						familySymbol = familySymbol2;
						break;
					}
				}
				hycomboBox.SelectedItem = cableTrayFitting.SelectFamily.Name + ":" + familySymbol.Name;
			}
		}

		private List<string> GetRevitDocFitting(YJKComboBox cmb)
		{
			List<string> list = new List<string>();
			foreach (object obj in cmb.Items)
			{
				string item = obj.ToString().Substring(0, obj.ToString().IndexOf(":"));
				list.Add(item);
			}
			return list;
		}

		private string GetFittingTypeName(string btnName, ref YJKComboBox cmb)
		{
			string result = "";
			uint num = 0;//<PrivateImplementationDetails>.ComputeStringHash(btnName);
			if (num <= 2289446842u)
			{
				if (num <= 1806103224u)
				{
					if (num != 113946616u)
					{
						if (num != 197302381u)
						{
							if (num == 1806103224u)
							{
								if (btnName == "btnTransition")
								{
									result = "过渡件";
									cmb = this.cbxTransition;
								}
							}
						}
						else if (btnName == "btnHTee")
						{
							result = "水平三通";
							cmb = this.cbxHTee;
						}
					}
					else if (btnName == "btnVInnerElbow")
					{
						result = "垂直内弯头";
						cmb = this.cbxVInnerElbow;
					}
				}
				else if (num != 1930737931u)
				{
					if (num != 2093867299u)
					{
						if (num == 2289446842u)
						{
							if (btnName == "btnVDownElbow")
							{
								result = "垂直下弯头";
								cmb = this.cbxVDownElbow;
							}
						}
					}
					else if (btnName == "btnHCross")
					{
						result = "水平四通";
						cmb = this.cbxHCross;
					}
				}
				else if (btnName == "btnVDownTee")
				{
					result = "下垂直三通";
					cmb = this.cbxVDownTee;
				}
			}
			else if (num <= 2494686189u)
			{
				if (num != 2345004235u)
				{
					if (num != 2416717256u)
					{
						if (num == 2494686189u)
						{
							if (btnName == "btnVUpElbow")
							{
								result = "垂直上弯头";
								cmb = this.cbxVUpElbow;
							}
						}
					}
					else if (btnName == "btnHElbow")
					{
						result = "水平弯头";
						cmb = this.cbxHElbow;
					}
				}
				else if (btnName == "btnVUpCornerTee")
				{
					result = "上角垂直三通";
					cmb = this.cbxVUpCornerTee;
				}
			}
			else if (num <= 3661444365u)
			{
				if (num != 3132574244u)
				{
					if (num == 3661444365u)
					{
						if (btnName == "btnVUpCornerCross")
						{
							result = "上角垂直四通";
							cmb = this.cbxVUpCornerCross;
						}
					}
				}
				else if (btnName == "btnVUpTee")
				{
					result = "上垂直三通";
					cmb = this.cbxVUpTee;
				}
			}
			else if (num != 3805147037u)
			{
				if (num == 3873352548u)
				{
					if (btnName == "btnVDownCornerTee")
					{
						result = "下角垂直三通";
						cmb = this.cbxVDownCornerTee;
					}
				}
			}
			else if (btnName == "btnVOuterElbow")
			{
				result = "垂直外弯头";
				cmb = this.cbxVOuterElbow;
			}
			return result;
		}

		private void FillCableTray()
		{
			foreach (Element element in this.cableTrayTypes)
			{
				string text = element.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString();
				if (!string.IsNullOrEmpty(text) && !this.cbxCableTray.Items.Contains(text))
				{
					this.cbxCableTray.Items.Add(text);
				}
			}
			if (this.cbxCableTray.Items.Count > 0)
			{
				this.cbxCableTray.SelectedIndex = 0;
			}
		}

		private void FillCableTrayFittingEntity()
		{
			if (File.Exists(this.path))
			{
				this.root = XElement.Load(this.path);
			}
			this.cableTrayTypes = RevitFilter.GetElementsOfType(this.doc, typeof(CableTrayType)).ToList<Element>();
			foreach (Element element in this.cableTrayTypes)
			{
				CableTrayFittingEntity cableTrayFittingEntity = new CableTrayFittingEntity();
				CableTrayType ctt = element as CableTrayType;
				ElementId id = ctt.get_Parameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_HORIZONTAL_BEND_PARAM).AsElementId();
				ElementId id2 = ctt.get_Parameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_ELBOWUP_PARAM).AsElementId();
				ElementId id3 = ctt.get_Parameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_ELBOWDOWN_PARAM).AsElementId();
				ElementId id4 = ctt.get_Parameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_TRANSITION_PARAM).AsElementId();
				cableTrayFittingEntity.CableTrayType = ctt.Name;
				cableTrayFittingEntity.HorizontalElbow = this.GetFittingInfo(id);
				cableTrayFittingEntity.VerticalInnerElbow = this.GetFittingInfo(id2);
				cableTrayFittingEntity.VerticalOuterElbow = this.GetFittingInfo(id3);
				cableTrayFittingEntity.Transition = this.GetFittingInfo(id4);
				if (ctt.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString() == "带配件的电缆桥架")
				{
					ElementId id5 = ctt.get_Parameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_TEE_PARAM).AsElementId();
					ElementId id6 = ctt.get_Parameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_CROSS_PARAM).AsElementId();
					cableTrayFittingEntity.HorizontalTee = this.GetFittingInfo(id5);
					cableTrayFittingEntity.HorizontalCross = this.GetFittingInfo(id6);
				}
				if (this.root != null)
				{
					List<XElement> list = (from ele in this.root.Elements()
					where ele.Name == "CableTrayType" && ele.Attribute("Type").Value == ctt.Name
					select ele).ToList<XElement>();
					if (list.Count == 1)
					{
						List<XElement> list2 = list[0].Elements().ToList<XElement>();
						cableTrayFittingEntity.VerticalUpElbow = list2[0].Value;
						cableTrayFittingEntity.VerticalDownElbow = list2[1].Value;
						cableTrayFittingEntity.VerticalUpTee = list2[2].Value;
						cableTrayFittingEntity.VerticalDownTee = list2[3].Value;
						cableTrayFittingEntity.VerticalUpCornerTee = list2[4].Value;
						cableTrayFittingEntity.VerticalDownCornerTee = list2[5].Value;
						cableTrayFittingEntity.VerticalUpCornerCross = list2[6].Value;
					}
				}
				this.cableTrayFittingEntitys.Add(cableTrayFittingEntity);
			}
		}

		private string GetFittingInfo(ElementId id)
		{
			string result = null;
			if (id != null && id.IntegerValue != -1)
			{
				FamilySymbol familySymbol = this.doc.GetElement(id) as FamilySymbol;
				string name = familySymbol.Family.Name;
				string name2 = familySymbol.Name;
				result = name + ":" + name2;
			}
			return result;
		}

		private void cbxCableTray_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.FillCableTrayType();
			if (this.cbxCableTray.SelectedItem.ToString() == "带配件的电缆桥架")
			{
				this.IsSel(true);
				return;
			}
			this.IsSel(false);
		}

		private void IsSel(bool isTake)
		{
			this.cbxVUpElbow.Enabled = isTake;
			this.cbxVDownElbow.Enabled = isTake;
			this.cbxHTee.Enabled = isTake;
			this.cbxVUpTee.Enabled = isTake;
			this.cbxVDownTee.Enabled = isTake;
			this.cbxVUpCornerTee.Enabled = isTake;
			this.cbxVDownCornerTee.Enabled = isTake;
			this.cbxHCross.Enabled = isTake;
			this.cbxVUpCornerCross.Enabled = isTake;
			this.btnVUpElbow.Enabled = isTake;
			this.btnVDownElbow.Enabled = isTake;
			this.btnHTee.Enabled = isTake;
			this.btnVUpTee.Enabled = isTake;
			this.btnVDownTee.Enabled = isTake;
			this.btnVUpCornerTee.Enabled = isTake;
			this.btnVDownCornerTee.Enabled = isTake;
			this.btnHCross.Enabled = isTake;
			this.btnVUpCornerCross.Enabled = isTake;
		}

		private void FillCableTrayType()
		{
			if (this.cbxCableTray.Items.Count == 0)
			{
				return;
			}
			this.cbxCableTrayType.Items.Clear();
			foreach (Element element in this.cableTrayTypes)
			{
				string text = element.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString();
				if (!string.IsNullOrEmpty(text) && text == this.cbxCableTray.SelectedItem.ToString() && !this.cbxCableTrayType.Items.Contains(element.Name))
				{
					this.cbxCableTrayType.Items.Add(element.Name);
				}
			}
			if (this.cbxCableTrayType.Items.Count > 0)
			{
				this.cbxCableTrayType.SelectedIndex = 0;
			}
		}

		private void cbxCableTrayType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.FillFitting();
		}

		private void FillFitting()
		{
			int partType = 0;
			int partType2 = 0;
			int partType3 = 0;
			int partType4 = 0;
			int partType5 = 0;
			bool flag = false;
			string a = this.cbxCableTray.SelectedItem.ToString();
			CableTrayType cableTrayType = (from type in this.cableTrayTypes
			where type.Name == this.cbxCableTrayType.SelectedItem.ToString()
			select type).ToList<Element>()[0] as CableTrayType;
            if ((int)cableTrayType.ShapeType == 1)
			{
				partType = 35;
				partType2 = 36;
				partType3 = 39;
				if (a == "带配件的电缆桥架")
				{
					partType4 = 38;
					partType5 = 37;
					flag = true;
				}
			}
            else if ((int)cableTrayType.ShapeType == 2)
			{
				partType = 43;
				partType2 = 44;
				partType3 = 47;
				if (a == "带配件的电缆桥架")
				{
					partType4 = 46;
					partType5 = 45;
					flag = true;
				}
			}
			IList<Element> fittings = RevitFilter.GetElementsOfType(this.doc, typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_CableTrayFitting).ToElements();
			this.FillFittingFamilyType(fittings, partType, cableTrayType, CableTrayFittingType.HorizontalElbow, this.cbxHElbow);
			this.FillFittingFamilyType(fittings, partType2, cableTrayType, CableTrayFittingType.VerticalInnerElbow, this.cbxVInnerElbow);
			this.FillFittingFamilyType(fittings, partType2, cableTrayType, CableTrayFittingType.VerticalOuterElbow, this.cbxVOuterElbow);
			this.FillFittingFamilyType(fittings, partType3, cableTrayType, CableTrayFittingType.Transition, this.cbxTransition);
			if (flag)
			{
				this.FillFittingFamilyType(fittings, partType4, cableTrayType, CableTrayFittingType.HorizontalTee, this.cbxHTee);
				this.FillFittingFamilyType(fittings, partType5, cableTrayType, CableTrayFittingType.HorizontalCross, this.cbxHCross);
				this.FillFittingFamilyType(fittings, partType, cableTrayType, CableTrayFittingType.VerticalUpElbow, this.cbxVUpElbow);
				this.FillFittingFamilyType(fittings, partType, cableTrayType, CableTrayFittingType.VerticalDownElbow, this.cbxVDownElbow);
				this.FillFittingFamilyType(fittings, partType4, cableTrayType, CableTrayFittingType.VerticalUpTee, this.cbxVUpTee);
				this.FillFittingFamilyType(fittings, partType4, cableTrayType, CableTrayFittingType.VerticalDownTee, this.cbxVDownTee);
				this.FillFittingFamilyType(fittings, partType4, cableTrayType, CableTrayFittingType.VerticalUpCornerTee, this.cbxVUpCornerTee);
				this.FillFittingFamilyType(fittings, partType4, cableTrayType, CableTrayFittingType.VerticalDownCornerTee, this.cbxVDownCornerTee);
				this.FillFittingFamilyType(fittings, partType5, cableTrayType, CableTrayFittingType.VerticalUpCornerCross, this.cbxVUpCornerCross);
				return;
			}
			this.cbxHTee.Items.Clear();
			this.cbxHCross.Items.Clear();
		}

		private void FillFittingFamilyType(IList<Element> fittings, int partType, CableTrayType ctt, CableTrayFittingType cft, YJKComboBox cbx)
		{
			cbx.Items.Clear();
			List<Element> list = new List<Element>();
			foreach (Element element in fittings)
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				Family family = familySymbol.Family;
				if (family.get_Parameter(BuiltInParameter.FAMILY_CONTENT_PART_TYPE).AsInteger() == partType)
				{
					if (cft == CableTrayFittingType.HorizontalElbow || cft == CableTrayFittingType.HorizontalTee || cft == CableTrayFittingType.HorizontalCross)
					{
						if (this.IsHorizontalFitting(family))
						{
							cbx.Items.Add(family.Name + ":" + familySymbol.Name);
							list.Add(element);
						}
					}
					else if (cft == CableTrayFittingType.Transition)
					{
						cbx.Items.Add(family.Name + ":" + familySymbol.Name);
						list.Add(element);
					}
					else if (cft == CableTrayFittingType.VerticalInnerElbow || cft == CableTrayFittingType.VerticalOuterElbow)
					{
						if (this.IsHorizontalInnerOrOuterElbow(family))
						{
							cbx.Items.Add(family.Name + ":" + familySymbol.Name);
						}
					}
					else if (this.SelectFitting(cft, family))
					{
						cbx.Items.Add(family.Name + ":" + familySymbol.Name);
						list.Add(element);
					}
				}
			}
			foreach (Element item in list)
			{
				fittings.Remove(item);
			}
			this.SetDefaultValue(ctt, cft, cbx);
		}

		private void SetDefaultValue(CableTrayType ctt, CableTrayFittingType cft, YJKComboBox cbx)
		{
			foreach (CableTrayFittingEntity cableTrayFittingEntity in this.cableTrayFittingEntitys)
			{
				if (cableTrayFittingEntity.CableTrayType == ctt.Name)
				{
					switch (cft)
					{
					case CableTrayFittingType.HorizontalElbow:
						this.SelectDefaultValue(cbx, cableTrayFittingEntity.HorizontalElbow);
						return;
					case CableTrayFittingType.VerticalInnerElbow:
						this.SelectDefaultValue(cbx, cableTrayFittingEntity.VerticalInnerElbow);
						return;
					case CableTrayFittingType.VerticalOuterElbow:
						this.SelectDefaultValue(cbx, cableTrayFittingEntity.VerticalOuterElbow);
						return;
					case CableTrayFittingType.VerticalUpElbow:
						this.SelectDefaultValue(cbx, cableTrayFittingEntity.VerticalUpElbow);
						return;
					case CableTrayFittingType.VerticalDownElbow:
						this.SelectDefaultValue(cbx, cableTrayFittingEntity.VerticalDownElbow);
						return;
					case CableTrayFittingType.Transition:
						this.SelectDefaultValue(cbx, cableTrayFittingEntity.Transition);
						return;
					case CableTrayFittingType.HorizontalTee:
						this.SelectDefaultValue(cbx, cableTrayFittingEntity.HorizontalTee);
						return;
					case CableTrayFittingType.VerticalUpTee:
						this.SelectDefaultValue(cbx, cableTrayFittingEntity.VerticalUpTee);
						return;
					case CableTrayFittingType.VerticalDownTee:
						this.SelectDefaultValue(cbx, cableTrayFittingEntity.VerticalDownTee);
						return;
					case CableTrayFittingType.VerticalUpCornerTee:
						this.SelectDefaultValue(cbx, cableTrayFittingEntity.VerticalUpCornerTee);
						return;
					case CableTrayFittingType.VerticalDownCornerTee:
						this.SelectDefaultValue(cbx, cableTrayFittingEntity.VerticalDownCornerTee);
						return;
					case CableTrayFittingType.HorizontalCross:
						this.SelectDefaultValue(cbx, cableTrayFittingEntity.HorizontalCross);
						return;
					case CableTrayFittingType.VerticalUpCornerCross:
						this.SelectDefaultValue(cbx, cableTrayFittingEntity.VerticalUpCornerCross);
						return;
					default:
						return;
					}
				}
			}
		}

		private void SelectDefaultValue(YJKComboBox cbx, string sel)
		{
			if (!string.IsNullOrEmpty(sel))
			{
				cbx.SelectedItem = sel;
			}
		}

		private bool SelectFitting(CableTrayFittingType fittingType, Family family)
		{
			if (this.IsHorizontalFitting(family))
			{
				return false;
			}
			bool result = false;
			this.ts.Commit();
			List<ConnectorElement> list = (from ele in RevitFilter.GetElementsOfType(this.doc.EditFamily(family), typeof(ConnectorElement)).ToElements()
			select ele as ConnectorElement).ToList<ConnectorElement>();
			ConnectorElement verticalConnector = this.GetVerticalConnector(list);
			if (verticalConnector != null)
			{
				list.Remove(verticalConnector);
				if (list.Count == 1)
				{
					result = this.SelectElbowFitting(list, verticalConnector, fittingType);
				}
				else if (list.Count == 2)
				{
					result = this.SelectTeeFitting(list, verticalConnector, fittingType);
				}
				else if (list.Count == 3)
				{
					result = this.SelectCrossFitting(list, verticalConnector, fittingType);
				}
			}
			this.ts.Start();
			return result;
		}

		private bool SelectElbowFitting(List<ConnectorElement> connectorElements, ConnectorElement conn, CableTrayFittingType fittingType)
		{
			bool result = false;
			bool flag = conn.Origin.Z < connectorElements[0].Origin.Z;
			if (fittingType == CableTrayFittingType.VerticalUpElbow && flag)
			{
				result = this.IsVerticalIntersection(connectorElements[0], conn);
			}
			else if (fittingType == CableTrayFittingType.VerticalDownElbow && !flag)
			{
				result = this.IsVerticalIntersection(connectorElements[0], conn);
			}
			return result;
		}

		private bool IsHorizontalFitting(Family family)
		{
			bool result = true;
			this.ts.Commit();
			IList<Element> list = RevitFilter.GetElementsOfType(this.doc.EditFamily(family), typeof(ConnectorElement)).ToElements();
			ConnectorElement connectorElement = list[0] as ConnectorElement;
			foreach (Element element in list)
			{
				ConnectorElement connectorElement2 = element as ConnectorElement;
				if (connectorElement.Origin.Z != connectorElement2.Origin.Z)
				{
					result = false;
					break;
				}
			}
			this.ts.Start();
			return result;
		}

		private bool IsHorizontalInnerOrOuterElbow(Family family)
		{
			bool result = true;
			this.ts.Commit();
			IList<Element> list = RevitFilter.GetElementsOfType(this.doc.EditFamily(family), typeof(ConnectorElement)).ToElements();
			ConnectorElement connectorElement = list[0] as ConnectorElement;
			foreach (Element element in list)
			{
				ConnectorElement connectorElement2 = element as ConnectorElement;
				if (Math.Abs(connectorElement.Origin.Z - connectorElement2.Origin.Z) >= 0.1)
				{
					result = false;
					break;
				}
			}
			this.ts.Start();
			return result;
		}

		private bool IsVerticalIntersection(ConnectorElement conn1, ConnectorElement conn2)
		{
			bool result = false;
			Transform coordinateSystem = conn1.CoordinateSystem;
			Transform coordinateSystem2 = conn2.CoordinateSystem;
			double num = coordinateSystem.BasisZ.AngleTo(coordinateSystem2.BasisZ);
			if (Math.PI*.5 - num < 0.001)
			{
				result = true;
			}
			return result;
		}

		private bool SelectTeeFitting(List<ConnectorElement> connectorElements, ConnectorElement conn, CableTrayFittingType fittingType)
		{
			bool flag = false;
			if (CableTrayFittingType.VerticalUpTee == fittingType)
			{
				flag = this.IsVerticalUpOrDownTee(connectorElements, flag, connectorElements[0].Origin.Z > conn.Origin.Z);
			}
			else if (CableTrayFittingType.VerticalDownTee == fittingType)
			{
				flag = this.IsVerticalUpOrDownTee(connectorElements, flag, connectorElements[0].Origin.Z < conn.Origin.Z);
			}
			else if (CableTrayFittingType.VerticalUpCornerTee == fittingType)
			{
				if (connectorElements[0].Origin.Z == connectorElements[1].Origin.Z && conn.Origin.Z > connectorElements[0].Origin.Z && connectorElements[0].Width == conn.Width && conn.Width == conn.Height)
				{
					flag = this.IsVerticalIntersection(connectorElements[0], connectorElements[1]);
				}
			}
			else if (CableTrayFittingType.VerticalDownCornerTee == fittingType && connectorElements[0].Origin.Z == connectorElements[1].Origin.Z && conn.Origin.Z > connectorElements[0].Origin.Z)
			{
				flag = this.IsVerticalIntersection(connectorElements[0], connectorElements[1]);
			}
			return flag;
		}

		private bool SelectCrossFitting(List<ConnectorElement> connectorElements, ConnectorElement conn, CableTrayFittingType fittingType)
		{
			bool result = false;
			if (CableTrayFittingType.VerticalUpCornerCross == fittingType && connectorElements[0].Origin.Z == connectorElements[1].Origin.Z && connectorElements[1].Origin.Z == connectorElements[2].Origin.Z && conn.Origin.Z > connectorElements[0].Origin.Z)
			{
				List<ConnectorElement> list = new List<ConnectorElement>();
				Transform coordinateSystem = connectorElements[0].CoordinateSystem;
				Transform coordinateSystem2 = connectorElements[1].CoordinateSystem;
				Transform coordinateSystem3 = connectorElements[2].CoordinateSystem;
				if (coordinateSystem.BasisZ.IsAlmostEqualTo(-coordinateSystem2.BasisZ))
				{
					list.Add(connectorElements[2]);
					list.Add(connectorElements[0]);
					list.Add(connectorElements[1]);
				}
				else if (coordinateSystem.BasisZ.IsAlmostEqualTo(-coordinateSystem3.BasisZ))
				{
					list.Add(connectorElements[1]);
					list.Add(connectorElements[0]);
					list.Add(connectorElements[2]);
				}
				else if (coordinateSystem2.BasisZ.IsAlmostEqualTo(-coordinateSystem3.BasisZ))
				{
					list.Add(connectorElements[0]);
					list.Add(connectorElements[1]);
					list.Add(connectorElements[2]);
				}
				if (list.Count == 3)
				{
					result = this.IsVerticalIntersection(list[0], list[1]);
				}
			}
			return result;
		}

		private bool IsVerticalUpOrDownTee(List<ConnectorElement> connectorElements, bool isAdd, bool upOrDown)
		{
			if (Math.Abs(connectorElements[0].Origin.Z - connectorElements[1].Origin.Z) < 0.0001 && upOrDown)
			{
				Transform coordinateSystem = connectorElements[0].CoordinateSystem;
				Transform coordinateSystem2 = connectorElements[1].CoordinateSystem;
				if (coordinateSystem.BasisZ.IsAlmostEqualTo(-coordinateSystem2.BasisZ))
				{
					isAdd = true;
				}
			}
			return isAdd;
		}

		private ConnectorElement GetVerticalConnector(List<ConnectorElement> connectorElements)
		{
			ConnectorElement result = null;
			foreach (ConnectorElement connectorElement in connectorElements)
			{
				double num = connectorElement.CoordinateSystem.BasisZ.AngleTo(XYZ.BasisZ);
				if (Math.PI - num < 0.0001 || num < 0.0001)
				{
					result = connectorElement;
					break;
				}
			}
			return result;
		}

		private void RegisterSelEvent()
		{
			foreach (object obj in base.Controls)
			{
                System.Windows.Forms.Control control = (System.Windows.Forms.Control)obj;
				if (control is GroupBox)
				{
					foreach (object obj2 in control.Controls)
					{
                        System.Windows.Forms.Control control2 = (System.Windows.Forms.Control)obj2;
						if (control2 is Button)
						{
							if (control2.Text == "选项...")
							{
								(control2 as Button).Click += this.button_Click;
							}
						}
						else if (control2 is YJKComboBox)
						{
							(control2 as YJKComboBox).SelectedIndexChanged += this.cbx_SelectedIndexChanged;
						}
					}
				}
			}
		}

		private void cbx_SelectedIndexChanged(object sender, EventArgs e)
		{
			CableTrayType cableTrayType = (from type in this.cableTrayTypes
			where type.Name == this.cbxCableTrayType.SelectedItem.ToString()
			select type).ToList<Element>()[0] as CableTrayType;
			YJKComboBox hycomboBox = sender as YJKComboBox;
			string name = hycomboBox.Name;
			string text = hycomboBox.SelectedItem.ToString();
			foreach (CableTrayFittingEntity cableTrayFittingEntity in this.cableTrayFittingEntitys)
			{
				if (cableTrayFittingEntity.CableTrayType == cableTrayType.Name)
				{
					uint num = 0;//<PrivateImplementationDetails>.ComputeStringHash(name);
					if (num <= 1864369088u)
					{
						if (num <= 687278177u)
						{
							if (num != 176365004u)
							{
								if (num != 596805998u)
								{
									if (num != 687278177u)
									{
										break;
									}
									if (!(name == "cbxVDownElbow"))
									{
										break;
									}
									cableTrayFittingEntity.VerticalDownElbow = text;
									break;
								}
								else
								{
									if (!(name == "cbxVUpCornerCross"))
									{
										break;
									}
									cableTrayFittingEntity.VerticalUpCornerCross = text;
									break;
								}
							}
							else
							{
								if (!(name == "cbxVOuterElbow"))
								{
									break;
								}
								cableTrayFittingEntity.VerticalOuterElbow = text;
								break;
							}
						}
						else if (num != 1366220999u)
						{
							if (num != 1453699000u)
							{
								if (num != 1864369088u)
								{
									break;
								}
								if (!(name == "cbxVDownTee"))
								{
									break;
								}
								cableTrayFittingEntity.VerticalDownTee = text;
								break;
							}
							else
							{
								if (!(name == "cbxHCross"))
								{
									break;
								}
								cableTrayFittingEntity.HorizontalCross = text;
								break;
							}
						}
						else
						{
							if (!(name == "cbxHElbow"))
							{
								break;
							}
							cableTrayFittingEntity.HorizontalElbow = text;
							break;
						}
					}
					else if (num <= 3284428458u)
					{
						if (num != 1975172681u)
						{
							if (num != 3050047667u)
							{
								if (num != 3284428458u)
								{
									break;
								}
								if (!(name == "cbxHTee"))
								{
									break;
								}
								cableTrayFittingEntity.HorizontalTee = text;
								break;
							}
							else
							{
								if (!(name == "cbxVUpTee"))
								{
									break;
								}
								cableTrayFittingEntity.VerticalUpTee = text;
								break;
							}
						}
						else
						{
							if (!(name == "cbxVInnerElbow"))
							{
								break;
							}
							cableTrayFittingEntity.VerticalInnerElbow = text;
							break;
						}
					}
					else if (num <= 3726449964u)
					{
						if (num != 3300916747u)
						{
							if (num != 3726449964u)
							{
								break;
							}
							if (!(name == "cbxVUpCornerTee"))
							{
								break;
							}
							cableTrayFittingEntity.VerticalUpCornerTee = text;
							break;
						}
						else
						{
							if (!(name == "cbxTransition"))
							{
								break;
							}
							cableTrayFittingEntity.Transition = text;
							break;
						}
					}
					else if (num != 3873352548u)
					{
						if (num != 4151515650u)
						{
							break;
						}
						if (!(name == "cbxVUpElbow"))
						{
							break;
						}
						cableTrayFittingEntity.VerticalUpElbow = text;
						break;
					}
					else
					{
						if (!(name == "btnVDownCornerTee"))
						{
							break;
						}
						cableTrayFittingEntity.VerticalDownCornerTee = text;
						break;
					}
				}
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.root == null)
			{
				this.root = new XElement("Root");
			}
			else
			{
				this.root.RemoveAll();
			}
			this.cableTrayTypes = RevitFilter.GetElementsOfType(this.doc, typeof(CableTrayType)).ToList<Element>();
			foreach (Element element in this.cableTrayTypes)
			{
				CableTrayType cableTrayType = element as CableTrayType;
				foreach (CableTrayFittingEntity cableTrayFittingEntity in this.cableTrayFittingEntitys)
				{
					if (cableTrayFittingEntity.CableTrayType == cableTrayType.Name)
					{
						this.UpdateCableTrayFittingSelToRevit(cableTrayType, cableTrayFittingEntity);
						XElement xelement = new XElement("CableTrayType", new XAttribute("Type", cableTrayFittingEntity.CableTrayType));
						XElement content = new XElement(CableTrayFittingType.VerticalUpElbow.ToString(), (cableTrayFittingEntity.VerticalUpElbow == null) ? "" : cableTrayFittingEntity.VerticalUpElbow);
						xelement.Add(content);
						content = new XElement(CableTrayFittingType.VerticalDownElbow.ToString(), (cableTrayFittingEntity.VerticalDownElbow == null) ? "" : cableTrayFittingEntity.VerticalDownElbow);
						xelement.Add(content);
						content = new XElement(CableTrayFittingType.VerticalUpTee.ToString(), (cableTrayFittingEntity.VerticalUpTee == null) ? "" : cableTrayFittingEntity.VerticalUpTee);
						xelement.Add(content);
						content = new XElement(CableTrayFittingType.VerticalDownTee.ToString(), (cableTrayFittingEntity.VerticalDownTee == null) ? "" : cableTrayFittingEntity.VerticalDownTee);
						xelement.Add(content);
						content = new XElement(CableTrayFittingType.VerticalUpCornerTee.ToString(), (cableTrayFittingEntity.VerticalUpCornerTee == null) ? "" : cableTrayFittingEntity.VerticalUpCornerTee);
						xelement.Add(content);
						content = new XElement(CableTrayFittingType.VerticalDownCornerTee.ToString(), (cableTrayFittingEntity.VerticalDownCornerTee == null) ? "" : cableTrayFittingEntity.VerticalDownCornerTee);
						xelement.Add(content);
						content = new XElement(CableTrayFittingType.VerticalUpCornerCross.ToString(), (cableTrayFittingEntity.VerticalUpCornerCross == null) ? "" : cableTrayFittingEntity.VerticalUpCornerCross);
						xelement.Add(content);
						this.root.Add(xelement);
						break;
					}
				}
			}
			this.ts.Commit();
			this.doc.Save();
			this.ts.Start();
			this.root.Save(this.path);
		}

		private void UpdateCableTrayFittingSelToRevit(CableTrayType ctt, CableTrayFittingEntity cfe)
		{
			string a = ctt.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString();
			this.UpdateCableTrayFiting(ctt, BuiltInParameter.RBS_CURVETYPE_DEFAULT_HORIZONTAL_BEND_PARAM, cfe.HorizontalElbow);
			this.UpdateCableTrayFiting(ctt, BuiltInParameter.RBS_CURVETYPE_DEFAULT_ELBOWUP_PARAM, cfe.VerticalOuterElbow);
			this.UpdateCableTrayFiting(ctt, BuiltInParameter.RBS_CURVETYPE_DEFAULT_ELBOWDOWN_PARAM, cfe.VerticalOuterElbow);
			this.UpdateCableTrayFiting(ctt, BuiltInParameter.RBS_CURVETYPE_DEFAULT_TRANSITION_PARAM, cfe.Transition);
			if (a == "带配件的电缆桥架")
			{
				this.UpdateCableTrayFiting(ctt, BuiltInParameter.RBS_CURVETYPE_DEFAULT_TEE_PARAM, cfe.HorizontalTee);
				this.UpdateCableTrayFiting(ctt, BuiltInParameter.RBS_CURVETYPE_DEFAULT_CROSS_PARAM, cfe.HorizontalCross);
			}
		}

		private void UpdateCableTrayFiting(CableTrayType ctt, BuiltInParameter param, string value)
		{
			if (string.IsNullOrEmpty(value))
			{
				return;
			}
			string b = value.Substring(0, value.LastIndexOf(":"));
			foreach (Element element in RevitFilter.GetElementsOfType(this.doc, typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_CableTrayFitting).ToElements())
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				if (familySymbol.Family.Name == b)
				{
					ctt.get_Parameter(param).Set(familySymbol.Id);
					break;
				}
			}
		}

		private Transaction ts;

		private UIApplication m_application;

		private Document doc;

		private List<CableTrayFittingEntity> cableTrayFittingEntitys = new List<CableTrayFittingEntity>();

		private List<Element> cableTrayTypes = new List<Element>();

		private XElement root;

		private string path = "C:\\Users\\NXQ\\Desktop\\temp.xml";

		private FamilyLibDatabase sysDB;

		private DbCategory dbCategory;
	}
}
